package com.idealighter.game.games.fruitslot.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import com.google.common.collect.Table;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.dic.FruitslotBonusNumCfgDic;
// import com.idealighter.game.dictionary.dic.FruitslotBonusIconDic;
// import com.idealighter.game.dictionary.dic.FruitslotBonusInnerCfgDic;
// import com.idealighter.game.dictionary.dic.FruitslotBonusNumCfgDic;
// import com.idealighter.game.dictionary.dic.FruitslotBonusOuterKickOutDic;
import com.idealighter.game.dictionary.dic.FruitslotIconDic;
// import com.idealighter.game.dictionary.domain.FruitslotBonusIconDomain;
// import com.idealighter.game.dictionary.domain.FruitslotBonusInnerCfgDomain;
// import com.idealighter.game.dictionary.domain.FruitslotBonusOuterKickOutDomain;
import com.idealighter.game.dictionary.domain.FruitslotIconDomain;
import com.idealighter.game.games.fruitslot.common.FruitslotReward;
import com.idealighter.game.games.fruitslot.common.RewardCalculator;
import com.idealighter.game.games.fruitslot.struct.FruitslotDifficulty;
import com.idealighter.game.games.fruitslot.struct.FruitslotSeat;
import com.idealighter.game.prizepool.manager.PlayerPrizePoolMgrScript;
import com.idealighter.game.prizepool.manager.RoomPrizePoolMgrScript;
import com.idealighter.game.prizepool.struct.control.FruitslotControl;
import com.idealighter.game.prizepool.struct.player.PlayerPrizePool;
import com.idealighter.game.prizepool.struct.room.ControlStrategy;
import com.idealighter.game.prizepool.struct.room.RoomPrizePool;
import com.idealighter.utils.code.RandCodeUtil;


@Singleton
public class FruitslotControlScript {


  private static final TypeReference<FruitslotControl> FRUITSLOT_CONTROL_TYPE =
      new TypeReference<FruitslotControl>() {};

  // 图标行数
  public static final int ICON_ROWS = 3;
  // 图标列数
  public static final int ICON_COLUMNS = 5;


  // @Inject
  // private FruitslotMsgMgr msgMgr;
  @Inject
  private FruitslotMgr fruitslotMgr;
  @Inject
  private FruitslotIconDic iconDic;
  // @Inject
  // private FruitslotBonusIconDic bonusIconDic;
  @Inject
  private FruitslotBonusNumCfgDic fruitslotBonusNumCfgDic;
  // @Inject
  // private FruitslotBonusOuterKickOutDic fruitslotBonusOuterKickOutDic;
  // @Inject
  // private FruitslotBonusInnerCfgDic fruitslotBonusInnerCfgDic;

  @Inject
  private PlayerPrizePoolMgrScript playerPrizePoolMgr;
  @Inject
  private RoomPrizePoolMgrScript roomPrizePoolMgr;


  /**
   * 控制水浒图标.
   * 
   * @param seat 座位.
   * @return 水浒得奖.
   */
  public FruitslotReward randomFruitslotIcons(FruitslotSeat seat) {
    /*
     * 个人奖池控制 .
     */
    PlayerPrizePool prizePool = playerPrizePoolMgr.prizePool(seat.getPlayerId(), Game.FRUIT_SLOT);
    if (!seat.isRobot() && prizePool != null && prizePool.getPrize().get() != 0) {
      return randomPlayerPrizePoolIcons(seat, prizePool);
    }

    /*
     * 房间奖池控制 .
     */
    long roomId = seat.getTable().getRoom().getId();
    RoomPrizePool roomPrizePool =
        roomPrizePoolMgr.roomPrizePool(seat.getPlayerId(), Game.FRUIT_SLOT, roomId);
    if (!seat.isRobot() && roomPrizePool != null) {
      return randomRoomPrizePoolIcons(seat, roomPrizePool);
    }

    // 正常概率图标
    return randomNormalFruitslotIcons(seat);
  }

  /**
   * 随机水浒图标.
   * 
   * @param seat 座位
   */
  public FruitslotReward randomNormalFruitslotIcons(FruitslotSeat seat) {
    Table<Integer, Integer, Integer> icons = seat.getIcons();

    for (int i = 0; i < ICON_ROWS; i++) {
      for (int j = 0; j < ICON_COLUMNS; j++) {
        // 合计权重
        int totalWeight = iconDic.getTotalWeight();
        // 随机数
        int rand = RandCodeUtil.random(totalWeight);
        int sum = 0;
        int icon = 0;

        for (FruitslotIconDomain iconDomain : iconDic.list()) {
          sum += iconDomain.getWeight();
          if (rand < sum) {
            icon = iconDomain.getId();
            break;
          }
        }

        icons.put(i, j, icon);
      }
    }

    return RewardCalculator.calculate(seat.getIcons(), fruitslotBonusNumCfgDic.map(),
        seat.getLine(), seat.getBet());
  }

  /**
   * 随机难度图标.
   * 
   * @param seat 座位
   * @param difficulty 难度
   * @return 奖励信息
   */
  private FruitslotReward randomDiffcultyIcons(FruitslotSeat seat, FruitslotDifficulty difficulty) {
    Table<Integer, Integer, Integer> icons = seat.getIcons();
    // 合计权重
    int totalWeight = difficulty.totalWeight();

    for (int i = 0; i < ICON_ROWS; i++) {
      for (int j = 0; j < ICON_COLUMNS; j++) {
        // 随机数
        int rand = RandCodeUtil.random(totalWeight);
        int sum = 0;
        int icon = 0;
        int[] weights = difficulty.getWeights();
        for (int k = 0; k < weights.length; k++) {
          int weight = weights[k];
          sum += weight;
          if (rand < sum) {
            icon = k + 1;
            break;
          }
        }

        icons.put(i, j, icon);
      }
    }

    return RewardCalculator.calculate(seat.getIcons(), fruitslotBonusNumCfgDic.map(),
        seat.getLine(), seat.getBet());
  }

  /**
   * 随机个人奖池水浒图标.
   * 
   * @param seat 座位信息
   * @param prizePool 奖池信息
   * @return 奖励信息
   */
  private FruitslotReward randomPlayerPrizePoolIcons(FruitslotSeat seat,
      PlayerPrizePool prizePool) {
    FruitslotControl fruitslotControl =
        JSON.parseObject(prizePool.getControl(), FRUITSLOT_CONTROL_TYPE);
    FruitslotDifficulty fruitslotDifficulty =
        FruitslotDifficulty.get(fruitslotControl.getDifficulty());

    // 图标奖励
    FruitslotReward fruitslotReward = randomDiffcultyIcons(seat, fruitslotDifficulty);
    long prize = prizePool.getPrize().get();
    long winGold = fruitslotMgr.convertGold(seat.getTable().getRoom().getRoomDomain(),
        fruitslotReward.getWin() - seat.getLine() * seat.getBet());

    if (prize > 0) {
      prize = prize - winGold;
      prize = prize < 0 ? 0 : prize;
    } else if (prize < 0) {
      prize = prize - winGold;
      prize = prize > 0 ? 0 : prize;
    }

    prizePool.getPrize().getAndSet(prize);
    return fruitslotReward;
  }

  /**
   * 水机房间奖池图片.
   * 
   * @param seat 座位信息
   * @param prizePool 奖池
   * @return 奖励
   */
  private FruitslotReward randomRoomPrizePoolIcons(FruitslotSeat seat, RoomPrizePool prizePool) {
    FruitslotReward fruitslotReward = null;

    // 房间奖池控制策略
    ControlStrategy strategy = prizePool.abledControlStrategy();
    if (strategy != null) {
      FruitslotControl control = strategy.control(FRUITSLOT_CONTROL_TYPE);
      FruitslotDifficulty fruitslotDifficulty = FruitslotDifficulty.get(control.getDifficulty());
      // 图标奖励
      fruitslotReward = randomDiffcultyIcons(seat, fruitslotDifficulty);
    } else {
      // 图标奖励
      fruitslotReward = randomNormalFruitslotIcons(seat);
    }

    long winGold = fruitslotMgr.convertGold(seat.getTable().getRoom().getRoomDomain(),
        fruitslotReward.getWin() - seat.getLine() * seat.getBet());
    prizePool.getPrize().addAndGet(-winGold);

    return fruitslotReward;
  }

  /**
   * 更新奖池信息.
   * 
   * @param seat 位置信息.
   * @param winGold 赢金币.
   */
  public void modifyPrizePoolAll(FruitslotSeat seat, long winGold) {
    /*
     * 个人奖池控制 .
     */
    PlayerPrizePool prizePool = playerPrizePoolMgr.prizePool(seat.getPlayerId(), Game.FRUIT_SLOT);
    Long prize = 0L;
    if (!seat.isRobot() && prizePool != null && (prize = prizePool.getPrize().get()) != 0) {
      if (prize > 0) {
        prize = prize - winGold;
        prize = prize < 0 ? 0 : prize;
      } else if (prize < 0) {
        prize = prize - winGold;
        prize = prize > 0 ? 0 : prize;
      }
      prizePool.getPrize().getAndSet(prize);
    }

    /*
     * 房间奖池控制 .
     */
    long roomId = seat.getTable().getRoom().getId();
    RoomPrizePool roomPrizePool =
        roomPrizePoolMgr.roomPrizePool(seat.getPlayerId(), Game.FRUIT_SLOT, roomId);
    if (!seat.isRobot() && roomPrizePool != null) {
      roomPrizePool.getPrize().addAndGet(-winGold);
    }
  }

}
